"use strict";
angular.module('app.protocol').controller('EditProtocolCtrl', function($scope,$http,$location,$cookieStore) {
	$scope.protocol_table = 'log_'+$location.search().port;
	//配置分页基本参数
    // $scope.paginationConf = {
    //     currentPage: 1,
    //     itemsPerPage: 10,
    //     pagesLength: 5,
    //     totalItems: 20,
    //     perPageOptions: [1, 2, 3, 40, 50],
    //     onChange: function () {
    //     }
    // };
    $scope.Length_dom = true;
    $scope.Length_dom124 = false;
    $scope.types = [
        {name:'VARCHAR',val:"字符串",length_unit:"字节"},
        { name: 'FLOAT', val: "浮点数", length_unit: "字节" },
        { name: 'INT', val: "整数", length_unit: "字节" }
    ];
    $scope.type = function (data) {
        if(data === "FLOAT"){
            $scope.Length_dom124 = false;
            $scope.Length_dom = false;
            $scope.Length = 4;
        }else if(data === "INT"){
            $scope.Length_dom = false;
            $scope.Length_dom124 = true;
            $(".btn_124").unbind('click');
            $(".btn_124").click(function(){
                $scope.Length = $(this).text()
            });
        }else if(data === "VARCHAR"){
            $scope.Length_dom = true;
            $scope.Length_dom124 = false;
        }
    };
    function getdata() {
        $http.get(baseurl+'/table_columns/'+$scope.protocol_table).then(function(res){
          //  console.log(res.data);
            if (res.data.ret == 1) {
                $scope.datas = res.data.info;
                for(var i = 0; i < $scope.datas.length; i++){
                    $scope.datas[i].status = false;
                    if($scope.datas[i].COLUMN_NAME == 'id' || $scope.datas[i].COLUMN_NAME == 'fid' || $scope.datas[i].COLUMN_NAME == 'projectid'|| $scope.datas[i].COLUMN_NAME == 'deviceid' || $scope.datas[i].COLUMN_NAME == 'time'){
                        $scope.datas[i].status = true;
                    }
                }
            }
        });
    }
    getdata();

    $scope.addobj = {};
    $scope.addobj.type = 'VARCHAR';

    // 添加一条字段
    $scope.sure_add = function () {

        $scope.addobj.type = $scope.addobj.type +"("+$scope.Length+")";
        if($scope.addobj.type && $scope.addobj.name && $scope.Length && !isNaN($scope.Length) && isNaN($scope.addobj.name)){

            $http.post(baseurl+"/add_column/"+$scope.protocol_table+"",$scope.addobj).then(function(res){
                $("#addAlert").modal("hide");
                new Notify("success","添加成功");
                getdata();
                $scope.addobj = {};
                $scope.addobj.type = 'VARCHAR';
                $scope.Length = null;
            });

        }else{
            new Notify("fail","添加失败");
        }
    };

    $scope.editobj = {};

    $scope.showEditAlert = function (data) {
        $scope.old = data.COLUMN_NAME;
        var sta = data.COLUMN_TYPE.indexOf("(");
        var end = data.COLUMN_TYPE.indexOf(")");

        $scope.editobj.name = data.COLUMN_NAME;
        $scope.editobj.type = data.DATA_TYPE.toUpperCase();
        $scope.editobj.des = data.column_comment;
        $scope.editLength = data.COLUMN_TYPE.substring(sta+1,end);

    };

    $scope.sure_edit = function () {
        $scope.editobj.type =  $scope.editobj.type +"("+$scope.editLength+")";

        if($scope.editobj.type && $scope.editobj.name && $scope.editLength && !isNaN($scope.editLength) && isNaN($scope.editobj.name)){

            $http.put(baseurl+"/column/"+$scope.protocol_table+"/"+$scope.old+"",$scope.editobj).then(function(res){
                $("#editAlert").modal("hide");
                new Notify("success","修改成功");
                getdata();
            });
        }else{
            new Notify("fail","修改失败")
        }
    };

    $scope.showDelDialog = function (name) {
        $scope.old = name;
    };

    $scope.sure_del = function () {
        $http.delete(baseurl+"/column/"+$scope.protocol_table+"/"+$scope.old+"").then(function(res){
            if(res.data.ret == 1){
                $("#delAlert").modal("hide");
                new Notify("success","删除成功");
                getdata();
            }else {
                new Notify("fail","删除失败")
            }

        });
    }

});